﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using EntityManage;
using BusinessFacade;

public partial class Modules_Personal_TargetAssignAppAdd : System.Web.UI.Page
{
    IPerf_Flow_TraceHandler traceHandler = new Perf_Flow_TraceManage();
    IPerf_FlowHandler flowHandler = new Perf_FlowManage();
    IPerf_Flow_StepsHandler stepsHandler = new Perf_Flow_StepsManage();
    ISys_UserHandler userHandler = new Sys_UserManage();
    IPerf_Flow_TraceDtlHandler traceDtlHandler = new Perf_Flow_TraceDtlManage();
    IPerf_Flow_TraceAttachHandler traceAttachHandler = new Perf_Flow_TraceAttachManage();

    IPerf_Personal_FirstAssignHandler faHandler = new Perf_Personal_FirstAssignManage();
    IPerf_Personal_ModTargetHandler modTargetHandler = new Perf_Personal_ModTargetManage();
    IPerf_Personal_ModTargetDtlHandler modTargetDtlHandler = new Perf_Personal_ModTargetDtlManage();
    ISys_OrganizationHandlerExt orgExtHandler = new Sys_OrganizationManageExt();
    IPerf_Personal_FinalAssignHandler finalAssignHandler = new Perf_Personal_FinalAssignManage();
    IPerf_Personal_FinalAssignDtlHandler faDtlHandler = new Perf_Personal_FinalAssignDtlManage();
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetOwnerHandler toHandler = new Perf_Personal_TargetOwnerManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("302031"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string[] ttIds = Request["ttIds"].Split(',');
        int userId = int.Parse(Session["CurrentUserID"].ToString());
        Sys_UserQuery userQuery = userHandler.GetSys_UserQuery(userId);

        HttpFileCollection files = HttpContext.Current.Request.Files;

        // 调用公共方法，处理流程
        bool isLastStep = false;
        int isPast = int.Parse(rblIsPass.SelectedValue);
        FlowTrace.ProcessFlowTraceStep(ttIds, txtComment.Text, isPast, userQuery, files,ref isLastStep);
        
        foreach (string tmpId in ttIds)
        {
            int traceId = int.Parse(tmpId);
            Perf_Flow_TraceQuery traceQuery = traceHandler.GetPerf_Flow_TraceQuery(traceId);
            Perf_Personal_ModTargetQuery modTargetQuery = modTargetHandler.GetPerf_Personal_ModTargetQuery(traceQuery.Business_ID);

            // 如果审批不通过，或者到了流程的最后一步，更新初次分配表和个别指标修改表的状态
            if (isPast != 0 || isLastStep == true)
            {
                modTargetQuery.Status = isPast == 0 ? (int)Enums.PersonalModTargetStatus.Pass : (int)Enums.PersonalModTargetStatus.NotPass;
                modTargetHandler.UpdatePerf_Personal_ModTarget(modTargetQuery);
            }

            // 如果流程最后一步，而且审批通过
            if (isLastStep == true && isPast == 0)
            {
                // 加到最终分配表，更新指标套表状态
                // 添加到最终分配表
                int finalAssignId = 0;
                Perf_Personal_FinalAssignQuery finalAssignQuery = new Perf_Personal_FinalAssignQuery();
                finalAssignQuery.User_ID = modTargetQuery.User_ID;
                finalAssignQuery.Target_User_ID = modTargetQuery.TargetUser_ID;
                finalAssignQuery.TargetOwner_ID = modTargetQuery.TargetOwner_ID;
                finalAssignQuery.Status = (int)Enums.PersonalFinalAssignStatus.NewAdded;
                finalAssignQuery.Create_Time = DateTime.Now;
                finalAssignQuery.Perf_Date = DateTime.Now;

                finalAssignHandler.InsertPerf_Personal_FinalAssign(finalAssignQuery, ref finalAssignId);
                // 添加最终分配子表
                if (finalAssignId != 0)
                {
                    // 查出该分配对应的指标
                    Perf_Personal_ModTargetDtlQuery[] modTargetDtlList = modTargetDtlHandler.GetPerf_Personal_ModTargetDtlQueryList(
                        " ModTarget_ID = @ModTarget_ID"
                        , new SqlParameter[] { new SqlParameter("@ModTarget_ID", modTargetQuery.ID) });
                    if (modTargetDtlList != null && modTargetDtlList.Length > 0)
                    {
                        foreach (Perf_Personal_ModTargetDtlQuery modTargetDtl in modTargetDtlList)
                        {
                            Perf_Personal_FinalAssignDtlQuery faDtlQuery = new Perf_Personal_FinalAssignDtlQuery();
                            faDtlQuery.Final_Assign_ID = finalAssignId;
                            faDtlQuery.Target_ID = modTargetDtl.Target_ID;
                            faDtlHandler.InsertPerf_Personal_FinalAssignDtl(faDtlQuery);
                        }
                    }
                    // 修改个人用户分配ID
                    Sys_UserQuery user = userHandler.GetSys_UserQuery(modTargetQuery.TargetUser_ID);
                    user.Assign_ID = finalAssignId;
                    userHandler.UpdateSys_User(user);
                }
                // 更新指标套状态
                Perf_Personal_TargetOwnerQuery ownerQuery = toHandler.GetPerf_Personal_TargetOwnerQuery(modTargetQuery.TargetOwner_ID);
                if (ownerQuery != null && ownerQuery.Status != (int)Enums.PersonalTargetOwnerStatus.Valid)
                {
                    ownerQuery.Status = (int)Enums.PersonalTargetOwnerStatus.Valid;
                    toHandler.UpdatePerf_Personal_TargetOwner(ownerQuery);
                }
            }
        }

        Jscript.ReturnAndClose(this.Page, "true");
    }

}
